package com.example.androidmainshixun.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.entity.Goods;
import com.example.androidmainshixun.entity.Goods1;

import java.util.List;

public class Adapter1 extends RecyclerView.Adapter<Adapter1.ViewHolder> {

    private List<Goods1> goodsList;

    public Adapter1(List<Goods1> goodsList) {
        this.goodsList = goodsList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 通过LayoutInflater加载item布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // 绑定数据到ViewHolder
        Goods1 goods1 = goodsList.get(position);
        // 假设Goods类中有getName和getImageResId方法
        holder.goodsName.setText(goods1.getName());
        holder.goodsPrice.setText(String.valueOf(goods1.getPrice()));
        holder.goodsPhoto.setImageResource(goods1.getImageResourceId());
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView goodsPhoto;
        TextView goodsName;
        TextView goodsPrice;

        public ViewHolder(View itemView) {
            super(itemView);
            // 初始化视图
            goodsPhoto =itemView.findViewById(R.id.goods_photo);
            goodsName = itemView.findViewById(R.id.goods_name);
            goodsPrice=itemView.findViewById(R.id.goods_price);
        }
    }
}